package util

import (
	"github.com/gogf/gf/v2/encoding/gjson"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/gogf/gf/v2/text/gstr"
	"github.com/gogf/gf/v2/util/gconv"
	"os/exec"
	"runtime"
)

// 打开系统默认浏览器
var ctx = gctx.New()

var commands = map[string]string{
	"windows": "start",
	"darwin":  "open",
	"linux":   "xdg-open",
}

func GetPort_client() string {
	httpport, _ := g.Cfg().Get(ctx, "server.Address_dynamicIpPc")
	return httpport.String()
}
func GetReload_nginxPc() string {
	temp, _ := g.Cfg().Get(ctx, "server.Reload_nginxPc")
	return temp.String()
}
func GetPort_server() string {
	httpport, _ := g.Cfg().Get(ctx, "server.Address_nginxPc")
	return httpport.String()
}
func GetYunserverIP() string {
	httpport, _ := g.Cfg().Get(ctx, "myconfig.my_yunserver_ip")
	return httpport.String()
}

func Get_sign_key() string {
	sign_key, _ := g.Cfg().Get(ctx, "myconfig.sign_key")
	return sign_key.String()
}

func Get_tcp_port() int {
	tcp_port, _ := g.Cfg().Get(ctx, "myconfig.tcp_port")
	return tcp_port.Int()
}
func Get_huanhang() string {
	return "\r\n"

	system := runtime.GOOS
	switch system {
	case "windows":
		{
			return "\r\n"
		}
	case "linux":
		{
			return "\r"

		}
	default:
		return "\n"
	}
}
func Get_nginx_confgs() []string {
	get, _ := g.Cfg().Get(ctx, "myconfig.nginx_conf_paths")
	array := get.Array()
	return gconv.SliceStr(array)
}
func Get_max_retry() int {
	sign_key, _ := g.Cfg().Get(ctx, "myconfig.max_retry")
	return sign_key.Int()
}

func Get_heart_time() int {
	heart_time, _ := g.Cfg().Get(ctx, "myconfig.heart_time")
	return heart_time.Int()
}

func GetThisServerIP() string {
	ip := g.Client().GetContent(ctx, "http://pv.sohu.com/cityjson?ie=utf-8")

	g.Log().Debug(ctx, "获取ip:"+ip)
	if len(ip) < 1 || gstr.Contains(ip, "403 Forbidden") {
		return GetThisServerIP()
	} else {
		ip = gstr.Trim(gstr.Split(gstr.Split(ip, "=")[1], ";")[0])

		jsonstr, _ := gjson.LoadJson(ip, true)
		back := jsonstr.Get("cip")
		return back.String()
	}
}
func OpenUrl(uri string) {
	system := runtime.GOOS
	switch system {
	case "windows":
		{
			// 无GUI调用
			exec.Command(`cmd`, `/c`, `start`, uri).Start()
			break
		}
	case "linux":
		{
			exec.Command(`xdg-open`, uri).Start()
			break
		}
	case "darwin":
		{
			exec.Command(`open`, uri).Start()
			break
		}
	}
}

/*func get_smtpConfigs() (string, string, string, string) {
	temp1 := g.Cfg().Get(ctx, "myconfig.smtpServer")
	temp2 := g.Cfg().Get(ctx, "myconfig.smtpUser")
	temp3 := g.Cfg().Get(ctx, "myconfig.smtpPassword")
	temp4 := g.Cfg().Get(ctx, "myconfig.smtpPort")

	return temp1, temp2, temp3, temp4
}*/

// 防止邮件发送异常，对邮寄发送的异常都归类为系统级异常，持久化
func SendMailGF_main(subject, content string) {
	/*server, user, passwd, _ := get_smtpConfigs()

	smtpConnection := gsmtp.New(server, user, passwd)
	sendMail := smtpConnection.SendMail("2642000280@qq.com", "310336951@qq.com", subject, content)
	g.Log().Line(true).Info("发送邮件:", subject, content, " 发送状态:", sendMail)*/
}
